package com.kuang.user.service;

import com.kuang.bascic.Myexception.BusinessException;
import com.kuang.bascic.query.BaseQuery;
import com.kuang.bascic.service.BaseServiceImpl;
import com.kuang.bascic.utils.MD5Utils;
import com.kuang.bascic.utils.PageList;
import com.kuang.user.domian.LoginInfo;
import com.kuang.user.domian.User;
import com.kuang.user.domian.dto.UserDto;
import com.kuang.user.mapper.LoginInfoMapper;
import com.kuang.user.mapper.UserMapper;
import io.netty.util.internal.StringUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private LoginInfoMapper loginInfoMapperl;

    @Override
    public User checkPhoneL(String phone) {
      return
              userMapper.checkPhoneL(phone);
    }

    @Override
    @Transactional
    public void regins(UserDto userDto) {
        /**
         *     CodeType: "REGINSTER"
         *     code: "24234"
         *     password: "24234"
         *     passwordRepeat: "2423424"
         *     phone: "32424324"
         *      两次事务的调用 所以加上事务传递机制
         */
        //数据校验
        verifyData(userDto);
        //把userdao---》转换为user对象
        // 整合usedao为user存进数据库
        User user = userDaoToUser(userDto);
        //user--->赋值到logininfo
        LoginInfo loginInfo = new LoginInfo();
        BeanUtils.copyProperties(user,loginInfo);
        loginInfo.setDisable(1);
        loginInfo.setType(1);
        loginInfo.setEmail(userDto.getEmail());
        loginInfoMapperl.addDepartment(loginInfo);
        //存储User  和Logininfo
        user.setLogininfo_id(loginInfo.getId());
        user.setEmail(userDto.getEmail());
        userMapper.addDepartment(user);
    }

    @Override
    public User selectUserByPhone(String phone) {

        return  userMapper.selectUserByPhone(phone);
    }

    private User userDaoToUser(UserDto userDto) {
        User user = new User();
        user.setPhone(userDto.getPhone());
        user.setUsername("p_"+userDto.getPhone());
        // 盐值 随机生成 把盐值设置到用户里 登录验证就拼接盐值
        String salt = UUID.randomUUID().toString();
        user.setSalt(salt);
//        private String password;
        String ByMD5 = MD5Utils.encrypByMd5(userDto.getPassword() + salt);
        user.setPassword(ByMD5);
        // 状态
        user.setState(1);
//        private Long logininfo_id;
        return user;
    }

    private void verifyData(UserDto userDto) {
        //数据校验 判断数据是否为空
        if (StringUtil.isNullOrEmpty(userDto.getCode()) ||
                StringUtil.isNullOrEmpty(userDto.getPassword()) ||
                StringUtil.isNullOrEmpty(userDto.getPasswordRepeat())||
                (StringUtil.isNullOrEmpty(userDto.getEmail()) && StringUtil.isNullOrEmpty(userDto.getPhone()))
        ) {
            throw new BusinessException("输入为空");
        }
        // 数据校验 判断两次密码不一致
        if (!userDto.getPassword().equals(userDto.getPasswordRepeat())) {
            throw new BusinessException("两次密码不一致");
        }
        // 判断验证码是否正确
        // 通过用户的电话和查询类型从redis中获取验证码 对比用户传的验证码  key  phone：type value code：时间戳
        if (userDto.getEmail()==null){
            String code = (String) redisTemplate.opsForValue().get(userDto.getPhone()+":" + userDto.getCodetype()
            );
        }
        String code = (String) redisTemplate.opsForValue().get(userDto.getEmail()+":" + userDto.getCodetype()
        );
        System.out.println(code);
        //判断用户穿的验证码和redis里面的验证码是否一致
        // 判断获取的是否为空  为空代表用户没点击获取就注册 不为空就和发送的验证码对比
        if (code==null) {
            throw new BusinessException("验证码错误");
        }else {
            // 点击了去和redis做对比
            // 截取出来的就是redis里面的验证码
            String code1 = code.split(":")[0];
            if (!userDto.getCode().equals(code1)) {
                throw new BusinessException("验证码错误");
            }}
    }

























}

